//
//  TXAuthoriztionCheck.m
//  BaiLeTou
//
//  Created by toro宇 on 2017/10/26.
//  Copyright © 2017年 yijie. All rights reserved.
// 对用户权限判断

#import "TXAuthoriztionCheck.h"
#import <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>
#import "TXAuthoriztionCheck.h"
//#import "UIViewController+Extension.h"


@implementation TXAuthorizationCheck 

+ (void)checkCameraAuthorization:(IsAllowBlock)isAllowBlock;
{
    AVAuthorizationStatus authorizationStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authorizationStatus == AVAuthorizationStatusNotDetermined) {
        // 还没询问
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            dispatch_async(dispatch_get_main_queue(), ^{
                isAllowBlock(granted);
            });
        }];
       
    }else if (authorizationStatus == AVAuthorizationStatusRestricted)
    {
        isAllowBlock(NO);
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"由于系统原因，用户不能访问摄像头" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertC addAction:action];
        [[UIViewController topVC] presentViewController:alertC animated:YES completion:nil];
        
    }else if (authorizationStatus == AVAuthorizationStatusDenied)
    {
        isAllowBlock(NO);
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请去-> [设置 - 隐私 - 相机 - 百格活动] 打开访问开关" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertC addAction:action];
        [[UIViewController topVC] presentViewController:alertC animated:YES completion:^{
            
        }];
    }else
    {
        isAllowBlock(YES);

    }
}

+ (void)chekcPhotoAuthorizatin:(IsAllowBlock)isAllowBlock
{
    PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus];
    if (authorizationStatus == PHAuthorizationStatusNotDetermined) {
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            dispatch_async(dispatch_get_main_queue(), ^{
                isAllowBlock(status);
            });
        }];
    }else if (authorizationStatus == PHAuthorizationStatusRestricted)
    {
         isAllowBlock(NO);
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"由于系统原因，用户不能访问相册" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertC addAction:action];
        [[UIViewController topVC] presentViewController:alertC animated:YES completion:nil];

    }else if (authorizationStatus == PHAuthorizationStatusDenied){
        isAllowBlock(NO);

        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请去-> [设置 - 隐私 - 相册 - 百格活动] 打开访问开关" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertC addAction:action];
        [[UIViewController topVC] presentViewController:alertC animated:YES completion:^{
            
        }];
    }else
    {
        isAllowBlock(YES);
    }
  
}


@end
